import java.io.File;
import commons.CompressionAlgorithm;
import commons.CompressionType;

import lz77.LZ77;
import huffman.HuffmanArchiver;


/**
 * 
 * @author Petro Slyvko, Gregor Solotarow
 */
public class Archiver
{

	/**
	 * Compress methods
	 */
	static LZ77				lz77Method		= null;
	static HuffmanArchiver	huffmanMethod	= null;

	public static void main( String[] args )
	{
		System.out.println( ":: Archiver based on Huffman/LZSS compress methods ::" );
		if ( args.length < 3 )
		{
			help();
			System.out.println( "Not enough arguments!" );
			return;
		}

		CompressionType type = null;
		CompressionAlgorithm algorithm = null;

		try
		{
			type = CompressionType.valueOf( args[ 0 ].substring( 2 ).toUpperCase() );
			algorithm = CompressionAlgorithm.valueOf( args[ 1 ].substring( 2 ).toUpperCase() );
		}
		catch ( IllegalArgumentException illegalArgument )
		{
			System.out.println( "Wrong parameter: "
				+ illegalArgument.getMessage() );
			help();
			return;
		}

		File archiveFile = new File( args[ 2 ] );

		if ( !archiveFile.exists() )
		{
			System.out.println( "ERROR: The given file does not exist. Check the file path!" );
			return;
		}
		
		try
		{
			// use the given method
			switch ( algorithm )
			{
				case HUFFMAN:

					if ( type == CompressionType.COMPRESS )
					{
						System.out.println( "Compressing file..." );
						HuffmanArchiver.compress( archiveFile );
						System.out.println( "Compression finished!" );
					}
					else if ( type == CompressionType.DECOMPRESS )
					{
						System.out.println( "Decompressing file..." );
						HuffmanArchiver.decompress( archiveFile );
						System.out.println( "Decompression finished!" );
					}
					break;
				case LZSS:
					lz77Method = new LZ77();
					// TODO: not implemented correctly yet
					break;
				default:
					break;
			}
		}
		catch ( Exception ex )
		{
			System.out.println( ex.getMessage() );
		}
	}

	public static void help()
	{
		System.out.println( "USAGE: java archiver --compress | --decompress --huffman | --lzss <FILE>" );
	}

}
